package org.mbds.wolf.tools;

import java.math.BigInteger;

import org.mbds.wolf.exception.ByteArrayToHexaStringException;
import org.mbds.wolf.exception.HexaStringToByteArrayException;

public class BytesTool {
	/**
	 * Hexadecimal String to byte array
	 * @param s
	 * @return
	 * @throws Exception
	 */
	public static byte[] hexStringToByteArray(String s) throws HexaStringToByteArrayException 
	{
//		System.out.println("Data to convert " + s);
		if(s.length()==1)
		{
			s= "0"+s;
		}
		byte[] data = null;
		try
		{
			byte[] bts = new BigInteger(s, 16).toByteArray();
			if(s.length()%2!=0 || s.length()==0)
			{
				throw new HexaStringToByteArrayException();
			}
			int lenght = s.length()/2; 
			data = new byte[lenght];
			if(lenght<bts.length)
			{
				System.arraycopy(bts, 1, data, 0, data.length);
			}
			else
			{
				System.arraycopy(bts, 0, data, 0, data.length);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return data;
	}
	
	/**
	 * byteToHexaString
	 * @param b
	 * @return
	 */
	public static String byteToHexaString(byte b) 
	{
		StringBuilder sb = new StringBuilder(2);
		sb.append(String.format("%02X", b));
		return sb.toString(); 
	}
	
	/**
	 * Byte array to Hexadecimal String
	 * @param bArray
	 * @return
	 * @throws Exception
	 */
	public static String byteArrayToHexString(byte[] bArray) throws ByteArrayToHexaStringException 
	{
		if (bArray.length==0) 
		{
			throw new ByteArrayToHexaStringException();
		}
		StringBuilder sb = new StringBuilder(bArray.length * 2);
	    for (byte b : bArray) 
	    {
    		sb.append(String.format("%02X ", b));
	    }	
	    return sb.toString(); 
	}
	
	/**
	 * byteArrayToIntValue
	 * @param bArray
	 * @return
	 */
	public static int byteArrayToIntValue(byte[] bArray) 
	{
		String str  = "FF";
		try 
		{
			str = byteArrayToHexString(bArray).replaceAll(" ", "");
//			System.out.println("WolfAndroidHce: BytesTool.byteArrayToIntValue (hexa): "+str);
//			System.out.println("WolfAndroidHce: BytesTool.byteArrayToIntValue (int): "+Integer.parseInt(str, 16));
		} 
		catch (ByteArrayToHexaStringException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Integer.valueOf(Integer.parseInt(str, 16));
	}
	
	/**
	 * integerToByteArray
	 * @param intVal
	 * @param size
	 * @return
	 */
	public static byte[] integerToByteArray(int intVal, int size) 
	{
		byte[] bArray = new byte[size];
		byte[] temp = integerToByteArray(intVal);
		if (temp.length>size)
		{
			System.arraycopy(temp, temp.length-size, bArray, 0, size);
		}
		else if (temp.length<size)
		{
			System.arraycopy(temp, 0, bArray, size-temp.length, temp.length);
		}
		else
		{
			bArray = temp;
		}
		return bArray;
	}

	/**
	 * integerToByteArray
	 * @param intVal
	 * @return
	 */
	public static byte[] integerToByteArray(int intVal) 
	{
		int length = 4;
		byte[] temp = new byte[length];
		temp[0] = (byte) ((intVal & 0xFF000000)>>0x24);
		temp[1] = (byte) ((intVal & 0x00FF0000)>>0x16);
		temp[2] = (byte) ((intVal & 0x0000FF00)>>0x8);
		temp[3] = (byte)  (intVal & 0x000000FF);
		if (temp[0]==0x00) length--; 
		if (temp[1]==0x00) length--; 
		if (temp[2]==0x00) length--; 
		byte[] result = new byte[length];
		System.arraycopy(temp, temp.length-length, result, 0, length);
		return result;
	}
}
